//
//  HBDiscoverItemHorizontalCell.m
//  HeartBeat
//
//  Created by karong on 2023/8/24.
//

#import "HBDiscoverItemHorizontalCell.h"
#import "HBDiscoverItemNovel3Cell.h"
#import "HBChannelItemModel.h"
#import "UIButton+ImageAlign.h"

@interface HBDiscoverItemHorizontalCell ()
@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIButton *moreBtn;
@property (strong, nonatomic) NSArray *dataArray;
@property (nonatomic, assign) CGSize itemSize;
@property (nonatomic, strong) HBChannelItemModel * object;
@end

@implementation HBDiscoverItemHorizontalCell
- (void)awakeFromNib {
    [super awakeFromNib];
    UINib * nib = [UINib nibWithNibName:@"HBDiscoverItemNovel3Cell" bundle:nil];
    [self.listView registerNib:nib forCellWithReuseIdentifier:@"HBDiscoverItemNovel3Cell"];
    [self.moreBtn setButtonImageTitleStyle:(WSButtonImageTitleStyleRight) padding:4];
}

- (void)setDataWithObject:(HBChannelItemModel *)object {
    self.object = object;
    self.dataArray = object.items;
    self.titleLabel.text = object.name;
    self.moreBtn.hidden = !object.is_more;
    self.listView.width = self.bounds.size.width;
    self.listView.height = self.bounds.size.height;
    CGFloat width = floor((HBScreenWidth - 24 - 24 - 12 - 12 - 12) / 4.0f);
    CGFloat height = width * 4 / 3.0 + 9 + 31;
    self.itemSize = CGSizeMake(width, height);
    [self.listView reloadData];
}

- (IBAction)moreAction:(id)sender {
    [HBEvenManager event:@"view_more" evenLogType:(HBEvenLogType_Click)
              attributes:@{@"Module_id":SF(@"%ld", (long)self.object.id)}];
}

#pragma mark - UICollectionViewDelete

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HBDiscoverItemNovel3Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HBDiscoverItemNovel3Cell" forIndexPath:indexPath];
    [cell setDataWithObject:self.dataArray[indexPath.row]];
    return cell;
}


#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return self.itemSize;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.delegate respondsToSelector:@selector(discoverItemHorizontalCellDidSelectWithObject:)]) {
        [self.delegate discoverItemHorizontalCellDidSelectWithObject:self.dataArray[indexPath.row]];
    }
}
@end
